package com.minsf.rpc;

import io.vertx.core.Vertx;

/**
 * @Description
 * @Author minify
 * @Date 2024/3/24 13:04
 * @Project sf-rpc
 */
public class VertxHttpServer implements HttpServer {


    @Override
    public void doStart(int port) {
        // 创建Vertx
        Vertx vertx = Vertx.vertx();

        // 创建 HTTP 服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

        // 绑定处理器
        server.requestHandler(new HttpServerHandler());

        // 监听端口并处理请求
        /*server.requestHandler(request -> {
            // 处理 HTTP 请求
            System.out.println("request received: " + request.method() + request.uri());

            // 发送 HTTP 响应
            request.response()
                    .putHeader("content-type", "text/plain")
                    .end("hello from vertx http server");
        });*/

        // 启动 HTTP 服务器并指定端口
        server.listen(port, result -> {
            if (result.succeeded()) {
                System.out.println("Server is now listening on "+ port);
            } else {
                System.out.println("Server failed start case " + result.cause());
            }
        });
    }
}
